<?php
/**
 * CreateTime: 2022/9/21 14:49
 */

namespace App\Service\Approval\ApprovalContract;

use App\Consts\Approval\ApprovalSubmitTypeConsts;
use App\Consts\ContractConsts\NewContractApprovalStatus;
use App\Models\NewContract\ContractModel;
use App\Service\Approval\DingTalkApprovalInterFace;
use App\Tools\Log\Log;

class ApprovalFwContract implements DingTalkApprovalInterFace
{

    public function approvalStart($processInstanceId, $sourceId, $processStatus)
    {
        $contractModel = new ContractModel();
        $status = $contractModel->getContractInfo($sourceId,'contract_status');
        if($status['contract_status'] != NewContractApprovalStatus::CONTRACT_APPROVAL_TO_DD_FW){
            return false;
        }
        // 修改合同审批状态为审批中
        try {
            $contractModel->editContract($sourceId,[
                'contract_status' =>NewContractApprovalStatus::CONTRACT_APPROVAL_FWSSZ
            ]);
        }catch (\Exception $exception){
            Log::e($exception);
        }
    }

    public function approvalAgreeEnd($processInstanceId, $sourceId, $processStatus)
    {
        $contractModel = new ContractModel();
        $status = $contractModel->getContractInfo($sourceId,'contract_status');
        if($status['contract_status']!=NewContractApprovalStatus::CONTRACT_APPROVAL_FWSSZ){
            return false;
        }
        // 修改合同状态为待送签
        $contractModel->editContract($sourceId,[
            'contract_status' =>NewContractApprovalStatus::CONTRACT_APPROVAL_DSS
        ]);
    }

    public function approvalRefuseEnd($processInstanceId, $sourceId, $processStatus)
    {
        $contractModel = new ContractModel();
        $status = $contractModel->getContractInfo($sourceId,'contract_status');
        if($status['contract_status']!=NewContractApprovalStatus::CONTRACT_APPROVAL_FWSSZ){
            return false;
        }
        $contractModel->editContract($sourceId,[
            'contract_status' =>NewContractApprovalStatus::CONTRACT_APPROVAL_ZF
        ]);
    }
}